const mongoose = require('mongoose')

// 订单模型
const orderSchema = new mongoose.Schema({
    // 订单号
    orderNumber: {
        type: String,
        required: true,
        unique: true,
        default: () => 'ORD' + Date.now() + Math.floor(Math.random() * 1000)
    },
    // 客户信息
    customer: {
        name: {
            type: String,
            required: true
        },
        phone: {
            type: String,
            required: true,
            validate: {
                validator: function(v) {
                    return /^1[3-9]\d{9}$/.test(v);
                },
                message: '手机号格式不正确'
            }
        },
        address: {
            type: String,
            required: true
        }
    },
    // 订单商品
    items: [{
        productId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Product',
            required: true
        },
        productName: {
            type: String,
            required: true
        },
        price: {
            type: Number,
            required: true,
            min: 0
        },
        quantity: {
            type: Number,
            required: true,
            min: 1
        },
        subtotal: {
            type: Number,
            required: true,
            min: 0
        }
    }],
    // 订单金额
    totalAmount: {
        type: Number,
        required: true,
        min: 0
    },
    // 配送费
    deliveryFee: {
        type: Number,
        default: 0,
        min: 0
    },
    // 优惠金额
    discountAmount: {
        type: Number,
        default: 0,
        min: 0
    },
    // 实际支付金额
    actualAmount: {
        type: Number,
        required: true,
        min: 0
    },
    // 订单状态
    status: {
        type: String,
        enum: ['待支付', '待发货', '配送中', '已完成', '已取消'],
        default: '待支付'
    },
    // 支付状态
    paymentStatus: {
        type: String,
        enum: ['未支付', '已支付', '已退款'],
        default: '未支付'
    },
    // 配送状态
    deliveryStatus: {
        type: String,
        enum: ['待发货', '配送中', '已送达', '配送失败'],
        default: '待发货'
    },
    // 支付方式
    paymentMethod: {
        type: String,
        enum: ['微信支付', '支付宝', '现金'],
        default: '微信支付'
    },
    // 订单备注
    remark: {
        type: String,
        maxlength: 500
    },
    // 创建时间
    orderTime: {
        type: Date,
        default: Date.now
    },
    // 支付时间
    paymentTime: {
        type: Date
    },
    // 发货时间
    shipmentTime: {
        type: Date
    },
    // 完成时间
    completionTime: {
        type: Date
    }
}, {
    timestamps: true,
    collection: 'orders'
});

module.exports = mongoose.model('Order', orderSchema);

// 患者信息模型（保留原有功能）
const patientSchema = new mongoose.Schema({
    // 手机号码
    tel: {
        type: Number,
    },
    // 患者头像
    avatar: {
        type: String,
    },
    // 患者姓名
    username: {
        type: String,
    },
    // 微信昵称
    wxname: {
        type: String
    },
    // 就诊卡号
    visitID: {
        type: Number,
    },
    // 证件类型
    IDcardtype: {
        type: String,
    },
    // 证件号码
    IDcartnumber: {
        type: String
    },
    // 就诊人地址
    address: {
        type: String
    },
    // 关系
    about: {
        type: String
    },
    // 就诊卡余额
    balance: {
        type: Number
    }
}, {
    collection: 'patients'
});

// 导出患者模型（兼容现有代码）
module.exports.Patient = mongoose.model('Patient', patientSchema);


